﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class views_addcart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        if (Request.QueryString["Cart"] != null)
        {
            AddCart();
        }
    }

    private void AddCart()
    {
        products gio = (products)Session["Cart"];

        //lay id tu commandagrument

        string id = Request.QueryString["Cart"].ToString();
        string Name = "", Image = "";
        long Price = 0, NewPrice = 0;
        int quant = 1;

        DataRow[] dr_product = ((DataTable)Cache.Get("products_view")).Select("productid = " + id);
        float discount;
        if (dr_product[0].ItemArray[12].ToString().Trim() != "")
        {

            discount = float.Parse(dr_product[0].ItemArray[12].ToString().Trim());
            Price = long.Parse(dr_product[0].ItemArray[10].ToString().Trim());
            NewPrice = long.Parse(((int.Parse(dr_product[0].ItemArray[10].ToString().Trim()) * discount) / 100).ToString());
        }
        else
        {
            NewPrice = long.Parse(dr_product[0].ItemArray[10].ToString().Trim());
        }

        Name = dr_product[0].ItemArray[1].ToString();
        Image = "images/products/" + dr_product[0].ItemArray[6].ToString();

        gio.Add(id, Name, quant, Price, NewPrice, Image);
        Response.Redirect("~/index.aspx?Cart=" + id.ToString());
    }
}